﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.SSO.Core.BusinessLogic;
using ESF.SSO.Core.Entities.BasicReferences;

#endregion


namespace ESF.SSO.Presenter.Admin.Enterprise
{
	public class EditEnterprisePresenter : BaseEditPresenter<IEditEnterpriseViewer, ESF.SSO.Core.Entities.Enterprise>
	{
		#region Constructor
		
		public EditEnterprisePresenter (IEditEnterpriseViewer pViewer) : base (pViewer) {}
		
		#endregion
		
		
		#region Funciones

		public override void Clear()
		{
			_Viewer.Code = string.Empty;
			_Viewer.Name = string.Empty;
			_Viewer.IdentificationTypeID = null;
			_Viewer.IdentificationNumber = string.Empty;
			_Viewer.IdentificationNumberDigit = string.Empty;
			
			_Viewer.UserCreationAlias = string.Empty;
			_Viewer.UserLastUpdateAlias = string.Empty;
			_Viewer.CreationDateTime = null;
			_Viewer.LastUpdateDateTime = null;
			
			_Viewer.ItemID = null;
			
			_Viewer.Location = null;
		}

		
		public override ESF.SSO.Core.Entities.Enterprise Get()
		{
			return CtrlEnterprise.Get (
				new ESF.SSO.Core.Entities.Enterprise () { ID = _Viewer.ItemID });
		}


		public override void PrepareFields()
		{
		}


		public override void Load (ESF.SSO.Core.Entities.Enterprise pItem)
		{
			_Viewer.Code = pItem.Code;
			_Viewer.Name = pItem.Name;
			_Viewer.IdentificationNumber = pItem.IdentificationNumber;
			_Viewer.IdentificationNumberDigit = pItem.IdentificationNumberDigit;
			_Viewer.IdentificationTypeID = pItem.IdentificationType.ID;
			
			_Viewer.CreationDateTime = pItem.CreationDateTime;
			_Viewer.LastUpdateDateTime = pItem.LastUpdateDateTime;
			_Viewer.UserLastUpdateAlias = pItem.UserLastUpdateAlias;
			_Viewer.UserCreationAlias = pItem.UserCreationAlias;
			
			_Viewer.Location = pItem.Location;

			_Viewer.Terminals = pItem.Terminals;
		}


		public override void Save()
		{
			ESF.SSO.Core.Entities.Enterprise enterprise;
		
		
			enterprise = new ESF.SSO.Core.Entities.Enterprise ()
			{
				ID = _Viewer.ItemID,
				Code = _Viewer.Code,
				Name = _Viewer.Name,
				IdentificationNumber = _Viewer.IdentificationNumber,
				IdentificationNumberDigit = _Viewer.IdentificationNumberDigit,
				IdentificationType = CtrlBasicReference.Get (new ESF.General.Entity.BasicReference () { ID = _Viewer.IdentificationTypeID }),
				
				CreationDateTime = DateTime.Now,
				UserCreationAlias = _Viewer.frmUserAlias,
				LastUpdateDateTime = DateTime.Now,
				UserLastUpdateAlias = _Viewer.frmUserAlias,
				
				Location = _Viewer.Location,

				Terminals = _Viewer.Terminals
			};
		
			try
			{
				enterprise.ID = CtrlEnterprise.Register (enterprise);
				
				_Viewer.ItemID = enterprise.ID;
				
				SetSavedMsg ();
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}
		
		#endregion
	}
}
